#!/usr/bin/env python

import sys

# When adding new devices to the list please keep them in alphabetical order
# board-name should contain 'dashes' (-) not 'underscores' (_) and shouldn't contain capitals
# you can check the formating using an online tool such as https://codebeautify.org/python-formatter-beautifier

#  'project' : {
#    'device' : {
#      'board-name' : {
#        'dtb' : 'board-name.dtb',
#        'config' : 'board_name_defconfig'
#      },
#    },
#  },

devices = \
{
  'Allwinner': {
    'A64': {
      'orangepi-win': {
        'dtb': 'sun50i-a64-orangepi-win.dtb',
        'config': 'orangepi_win_defconfig'
      },
      'pine64': {
        'dtb': 'sun50i-a64-pine64.dtb',
        'config': 'pine64_plus_defconfig'
      },
      'pine64-lts': {
        'dtb': 'sun50i-a64-pine64-lts.dtb',
        'config': 'pine64-lts_defconfig'
      },
      'pine64-plus': {
        'dtb': 'sun50i-a64-pine64-plus.dtb',
        'config': 'pine64_plus_defconfig'
      },
    },
    'H3': {
      'bananapi': {
        'dtb': 'sun7i-a20-bananapi.dtb',
        'config': 'Bananapi_defconfig'
       },
      'bananapi-m2p': {
        'dtb': 'sun8i-h3-bananapi-m2-plus.dtb',
        'config': 'bananapi_m2_plus_h3_defconfig'
      },
      'beelink-x2': {
        'dtb': 'sun8i-h3-beelink-x2.dtb',
        'config': 'beelink_x2_defconfig'
      },
      'cubieboard2': {
        'dtb': 'sun7i-a20-cubieboard2.dtb',
        'config': 'Cubieboard2_defconfig'
      },
      'cubietruck': {
        'dtb': 'sun7i-a20-cubietruck.dtb',
        'config' : 'Cubietruck_defconfig'
      },
      'libretech-h3': {
        'dtb': 'sun8i-h3-libretech-all-h3-cc.dtb',
        'config': 'libretech_all_h3_cc_h3_defconfig'
      },
      'nanopi-m1': {
        'dtb': 'sun8i-h3-nanopi-m1.dtb',
        'config': 'nanopi_m1_defconfig'
      },
      'nanopi-m1-plus': {
        'dtb': 'sun8i-h3-nanopi-m1-plus.dtb',
        'config': 'nanopi_m1_plus_defconfig'
      },
      'orangepi-2': {
        'dtb': 'sun8i-h3-orangepi-2.dtb',
        'config': 'orangepi_2_defconfig'
      },
      'orangepi-lite': {
        'dtb': 'sun8i-h3-orangepi-lite.dtb',
        'config': 'orangepi_lite_defconfig'
      },
      'orangepi-one': {
        'dtb': 'sun8i-h3-orangepi-one.dtb',
        'config': 'orangepi_one_defconfig'
      },
      'orangepi-pc': {
        'dtb': 'sun8i-h3-orangepi-pc.dtb',
        'config': 'orangepi_pc_defconfig'
      },
      'orangepi-pc-plus': {
        'dtb': 'sun8i-h3-orangepi-pc-plus.dtb',
        'config': 'orangepi_pc_plus_defconfig'
      },
      'orangepi-plus2e': {
        'dtb': 'sun8i-h3-orangepi-plus2e.dtb',
        'config': 'orangepi_plus2e_defconfig'
      },
      'orangepi-plus': {
        'dtb': 'sun8i-h3-orangepi-plus.dtb',
        'config': 'orangepi_plus_defconfig'
      },
    },
    'H6': {
      'orangepi-3': {
        'dtb': 'sun50i-h6-orangepi-3.dtb',
        'config': 'orangepi_3_defconfig'
      },
      'orangepi-one-plus': {
        'dtb': 'sun50i-h6-orangepi-one-plus.dtb',
        'config': 'orangepi_one_plus_defconfig'
      },
      'pine-h64': {
        'dtb': 'sun50i-h6-pine-h64.dtb',
        'config': 'pine_h64_defconfig'
      },
    },
  },
  'Amlogic': {
    'AMLGX': {
      'box': {
        'dtb': 'meson-gxl-s905x-p212.dtb',
        'config': 'p212_defconfig'
      },
      'lepotato': {
        'dtb': 'meson-gxl-s905x-libretech-cc.dtb',
        'config': 'libretech-cc_defconfig'
      },
      'nanopi-k2': {
        'dtb': 'meson-gxbb-nanopi-k2.dtb',
        'config': 'nanopi-k2_defconfig'
      },
      'odroid-c2': {
        'dtb': 'meson-gxbb-odroidc2.dtb',
        'config': 'odroid-c2_defconfig'
      },
    },
    'AMLG12': {
      'box': {
        'dtb': 'meson-gxl-s905x-p212.dtb',
        'config': 'p212_defconfig'
      },
    },
  },
  'Qualcomm': {
    'Dragonboard': {
      '410c': {
        'dtb': 'apq8016-sbc.dtb',
        'config': 'dragonboard410c_defconfig'
      },
    },
  },
  'Rockchip': {
    'MiQi': {
      'rk3288': {
        'dtb': 'rk3288-miqi.dtb',
        'config': 'miqi-rk3288_config'
      },
    },
    'RK3328': {
      'box': {
        'dtb': 'rk3328-box.dtb',
        'config': 'evb-rk3328_defconfig'
      },
      'box-trn9': {
        'dtb': 'rk3328-box-trn9.dtb',
        'config': 'evb-rk3328_defconfig'
      },
      'box-z28': {
        'dtb': 'rk3328-box-z28.dtb',
        'config': 'evb-rk3328_defconfig'
      },
      'roc-cc': {
        'dtb': 'rk3328-roc-cc.dtb',
        'config': 'evb-rk3328_defconfig'
      },
      'rock64': {
        'dtb': 'rk3328-rock64.dtb',
        'config': 'evb-rk3328_defconfig'
      },
      'rockbox': {
        'dtb': 'rk3328-rockbox.dtb',
        'config': 'evb-rk3328_defconfig'
      },
    },
    'RK3399': {
      'khadas-edge': {
        'dtb': 'rk3399-khadas-edge.dtb',
        'config': 'evb-rk3399_config'
      },
      'rock960': {
        'dtb': 'rk3399-rock960.dtb',
        'config': 'evb-rk3399_config'
      },
      'rock-pi-4': {
        'dtb': 'rk3399-rock-pi-4.dtb',
        'config': 'evb-rk3399_config'
      },
      'rockpro64': {
        'dtb': 'rk3399-rockpro64.dtb',
        'config': 'evb-rk3399_config'
      },
      'sapphire': {
        'dtb': 'rk3399-sapphire.dtb',
        'config': 'evb-rk3399_config'
      },
    },
    'TinkerBoard': {
      'rk3288': {
        'dtb': 'rk3288-miniarm.dtb',
        'config': 'tinker-rk3288_config'
      },
    },
  },
}

def usage():
  print(' '.join(['Usage:', sys.argv[0], '<project>', '<soc>', '<board-name>', 'dtb|config']))
  print(' '.join(['      ', sys.argv[0], '<project>', '<soc>']))
  print(' '.join(['      ', sys.argv[0], '<project>']) + '\n')
  print('Projects:' + '\n')
  for project in devices:
    print('  ' + project + ':')
    for soc in devices[project]:
      print('    ' + soc + ':')
      for board in devices[project][soc]:
        print('      ' + board)
      print('')
    print('')

# Get dtb or u-boot config for a given project, soc, and board
# ./scripts/uboot_helper project device board_name dtb
if len(sys.argv) == 5:
  if sys.argv[1] in devices:
    if sys.argv[2] in devices[sys.argv[1]]:
      if sys.argv[3] in devices[sys.argv[1]][sys.argv[2]]:
        if sys.argv[4] in ['dtb', 'config']:
          print(devices[sys.argv[1]][sys.argv[2]][sys.argv[3]][sys.argv[4]])
          sys.exit(0)

# List boards supported by a given project and soc
# ./scripts/uboot_helper project device
elif len(sys.argv) == 3:
  if sys.argv[1] in devices:
    if sys.argv[2] in devices[sys.argv[1]]:
      boards = []
      for board in sorted(devices[sys.argv[1]][sys.argv[2]]):
          boards.append(board)
      print(' '.join(boards))
  sys.exit(0)

# List socs supported by a given project
# ./scripts/uboot_helper project
elif len(sys.argv) == 2:
  if sys.argv[1] in ['help', 'usage']:
    usage()
  elif sys.argv[1] in devices:
    socs = []
    for soc in sorted(devices[sys.argv[1]]):
      socs.append(soc)
    print(' '.join(socs))
  sys.exit(0)

# List projects
# ./scripts/uboot_helper
elif len(sys.argv) == 1:
  projects = []
  for project in sorted(devices):
    projects.append(project)
  print(' '.join(projects))
  sys.exit(0)

usage()
